package com.glory.brepda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.entity.BinInfoEntity;

import java.text.NumberFormat;
import java.util.List;

/**
 * FileName: BinInfoListAdapter
 * Author: XJH
 * Date: 2020/5/29 15:25
 * Description:
 * History:
 */
public class OfflineBinInfoListAdapter extends BaseQuickAdapter<BinInfoEntity.BinInfo, BaseViewHolder> {
    private boolean isHaveRate = false;
    private int mMainQTY = -1;

    public OfflineBinInfoListAdapter(@Nullable List<BinInfoEntity.BinInfo> data, boolean isHaveRate) {
        super(R.layout.item_bin_info_list,data);
        this.isHaveRate = isHaveRate;
    }

    @Override
    protected void convert(BaseViewHolder helper, BinInfoEntity.BinInfo item) {
        helper.setText(R.id.list_tv1,item.getBINNO());
        helper.setText(R.id.list_tv2,item.getBINNAME());
        helper.setText(R.id.list_tv3,"");
        helper.setGone(R.id.list_tv3,false);
        helper.setGone(R.id.etBinQTY,true);

        helper.setGone(R.id.list_tv4,isHaveRate);
        calculation(helper.getView(R.id.list_tv4),item.getBINQTY());
        EditText etBinQTY = helper.getView(R.id.etBinQTY);
        etBinQTY.setText(String.valueOf(item.getBINQTY()));
        setEditListener(etBinQTY,item,helper.getView(R.id.list_tv4));
    }

    private void calculation(TextView view, double binQTY) {
        if (mMainQTY == -1){
            return;
        }
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(6);
        String result = numberFormat.format(((float) (binQTY) / (float) mMainQTY) * 100);
        view.setText(result + "%");
    }

    public void setMainQTY(int mainQTY){
        this.mMainQTY = mainQTY;
    }

    private void setEditListener(EditText editText, BinInfoEntity.BinInfo item,TextView view) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    item.setBINQTY(Integer.parseInt(s.toString()));
                    calculation(view,Double.parseDouble(s.toString()));
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
